import { createRouter, createWebHistory } from 'vue-router'
import BigScreen from '@/views/BigScreen.vue'
import InfoVis from '@/views/InfoVis.vue'
import DataManage from '@/views/DataManage.vue'
import Login from '@/views/Login.vue'
import Register from '@/views/Register.vue'



const routes=[
    {
      path: '/',
      name: 'home',
      redirect:"/bigscreen"
    },
    {
      path:'/bigscreen',
      name:'bigscreen',
      component: BigScreen,
      meta:{
        requestAuth:true
      }
    },
    {
      path: '/vis',
      name: 'infois',
      component: InfoVis,
      meta:{
        requestAuth:true
      }
    },
    {
      path: '/data',
      name: 'data',
      component: DataManage,
      meta:{
        requestAuth:true
      }
    },
    {
      path: '/404',
      name: '404',
      component: Error,
      meta:{
        requestAuth:false
      }
    },
    {
      path: '/login',
      name: 'login',
      component: Login,
      meta:{
        requestAuth:false
      }
    },
    {
      path: '/register',
      name: 'register',
      component: Register,
      meta:{
        requestAuth:false
      }
    }
]
const router=createRouter({
  history:createWebHistory(),
  routes
})


router.beforeEach((to,from,next)=>{
  let token=localStorage.getItem("token");
  if(to.meta.requestAuth && !token){
    next({name:"login"});
  }else{
    next();
  }
})
export default router
